<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
   <style>
       table{
           border: 5px dashed green;

       }
       table td{
           width: 74px;
           height: 30px;
           text-align: center;
           
           
       }
       table th{
        width: 74px;
           height: 50px;
       }
       button{
           outline: none;
           border: 0;
           border: 1px solid pink;
           background-color: orange;
           border-radius: 7px;
           color: #fff;
           height: 30px;
       }
   </style>
<body>
    <button id="addBtn">添加一条新的访客信息</button>
    <!-- 序列号	姓名	年龄	操作 -->
    <button id="affBtn">删除选中</button>
    <button id="aggBtn">年龄从小到大</button>
    <button id="ahhBtn">一键勾选</button>
    <button id="ajjBtn">一键取消勾选</button>
    <table border=1 cellspacing=0 cellpadding=10>
        <tbody>
            <tr>
                <th>序列号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>操作</th>
                <th>勾选</th>
            </tr>
            <!-- <input class="fxk" type="checkbox"> -->

            <!--
            <tr>
                <td>1</td>
                <td>胡杭</td>
                <td>16</td>
                <td><button>删除</button></td>
            </tr>
            -->
        </tbody>
    </table>


    <script>
        let info = [{
            name: "胡杭",
            age: 16
        }, {
            name: "胜明",
            age: 22
        }, {
            name: "军毅",
            age: 21
        }, {
            name: "晓华",
            age: 13
        }, {
            name: "盛聪",
            age: 23
        }, {
            name: "侦剑",
            age: 32
        }, {
            name: "红翔",
            age: 25
        }, {
            name: "超维",
            age: 18
        }, {
            name: "士琪",
            age: 22
        }, {
            name: "艳华",
            age: 20
        }];


        let tbody = document.querySelector("tbody");
        let addBtn = document.getElementById("addBtn");
        let affBtn = document.getElementById("affBtn");
        // console.log(affBtn);
        let aggBtn = document.getElementById("aggBtn");
        let ahhBtn = document.getElementById("ahhBtn");
        let ajjBtn = document.getElementById("ajjBtn");


        let delBtn = tbody.querySelectorAll("button");
        // let delBtn = tbody.getElementsByTagName("button");

        let index = 0; // 模拟点击下标(每次自增1)

        addBtn.onclick = function () {
            // console.log(index);
            // 当前index值要小于数组长度,否则会报错，获取不到数组的值
            if (index < info.length) {
                // 创建新的tr
                let newTr = document.createElement("tr");
                // 新的tr添加tr单元格
                newTr.innerHTML = `
                <td>${index + 1}</td>
                <td>${info[index%10].name}</td>
                <td>${info[index%10].age}</td>
                <td><button>删除</button></td>
                <td><input class="fxk"type="checkbox"></td>
                `;
                //  将新的tr添加到tbody
                console.log(index%10);
                tbody.appendChild(newTr);

                // 删除单个信息
                delBtn = tbody.querySelectorAll("button");
                // console.log(delBtn);
                // 每个删除按钮绑定点击事件
                delBtn.forEach(function (el) {
                    el.onclick = function () {
                        // el == this
                        // console.log(this.parentElement.parentElement.remove());
                        el.parentElement.parentElement.remove();
                    }
                });

                // console.log("before", delBtn);
                // for (let i = 0; i < delBtn.length; i++) {
                //     delBtn[i].onclick = function () {
                //         delBtn[i].parentElement.parentElement.remove();
                //         console.log("after", delBtn);
                //     }
                // }

                // 添加完毕后自增
                index++;
            }
            let fxk = document.querySelectorAll(".fxk")


            affBtn.onclick = function () {
                fxk.forEach(function (item) {
                    if (item.checked == true) {
                        console.log(item);
                        console.log(item.checked);
                        item.parentNode.parentNode.remove();
                    }
                })
            }



            aggBtn.onclick = function () {

                let trList = document.querySelectorAll("tr:not(:first-child)")
                // let newTrArr = [...trList]
                let newArr=[]
                trList.forEach(function(item){
                    // console.log(item);
                  newArr.push(item)
                })
                    // console.log(newArr);
                newArr.sort(function (a, b) {
                    // console.log(a.children[2].innerText);
                    // console.log(a);
                    // console.log(b);
                    return (a.children[2].innerText) - (b.children[2].innerText)

                })
                newArr.forEach(function(item){
                    // console.log(item);
                    tbody.appendChild(item)
                })

                // console.log(info);
            }



            ahhBtn.onclick = function (){
                 fxk.forEach(function(item){

                    item.checked=true;
                 })

            }


            ajjBtn.onclick = function (){
                 fxk.forEach(function(item){

                    item.checked=false;
                 })

            }

         

        }
    </script>
</body>

</html>